package com.zsummer.zero.hibernate;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.junit.Test;

/**
 * 使用c3p0作为连接池
 * 回滚
 * query
 * 分页
 * @author zhouyujiang
 *
 */
public class Demo2 {

	/**
	 * get
	 */
	@Test
	public void run1() {
		Session session = HibernateUtils.openSession();
		session.getTransaction().begin();
		
		Person person = session.get(Person.class, 10);
		System.out.println(person);
		
		session.getTransaction().commit();
		session.close();
		HibernateUtils.close();
	}
	
	/**
	 * rollback
	 */
	@Test
	public void run2() {
		Session session = HibernateUtils.openSession();
		session.getTransaction().begin();
		
		Person person = new Person();
		person.setName("小锤");
		person.setAge((short) 23);
		Serializable save = session.save(person);
		System.out.println(save);
		
		session.getTransaction().rollback();
		session.close();
		HibernateUtils.close();
	}
	
	/**
	 * query
	 */
	@Test
	public void run3() {
		Session session = HibernateUtils.openSession();
		session.getTransaction().begin();
		
//		Query<Person> query = session.createQuery("select p from Person p", Person.class);
//		List<Person> persons = query.list();
//		System.out.println(persons);
		
//		Query<Person> query = session.createQuery("from Person", Person.class);
//		List<Person> persons = query.list();
//		System.out.println(persons);
		
		// specified column
//		Query<Person> query = session.createQuery("select new Person(age) from Person", Person.class); // need param age of construct 
//		List<Person> persons = query.list();
//		System.out.println(persons);
		
		// add where clause 
//		Query<Person> query = session.createQuery("from Person where name=?0", Person.class); 
//		query.setParameter(0, "马师人");
//		List<Person> persons = query.list();
//		System.out.println(persons);
//		Query<Person> query = session.createQuery("from Person where name=:name", Person.class); 
//		query.setParameter("name", "马师人");
//		List<Person> persons = query.list();
//		System.out.println(persons);
//		Query<Person> query = session.createQuery("from Person where name=:name", Person.class); 
//		query.setParameter("name", "马师人");
//		Person person = query.uniqueResult();
//		System.out.println(person);
		
		// use local sql
//		@SuppressWarnings("unchecked")
//		NativeQuery<Person> query = session.createSQLQuery("select * from zyj_person where name = :name");
//		query.addEntity(Person.class);
//		query.setParameter("name", "马师人");
//		List<Person> persons = query.list();
//		System.out.println(persons);
		
		
		session.getTransaction().commit();
		session.close();
		HibernateUtils.close();
	}
	
	/**
	 * insert
	 */
	@Test
	public void run4() {
		String familyNames = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤";
		String names = "人之初性本善性相近习相远苟不教性乃迁教之道贵以专昔孟母择邻处子不学断机杼窦燕山有义方教五子名俱扬养不教父之过教不严师之惰子不学非所宜幼不学老何为玉不琢不成器人不学不知义为人子方少时亲师友习礼仪香九龄能温席孝于亲所当执";
		Random random = new Random();
		
		List<String> nameList = new ArrayList<String>();
		for (int i = 0; i < 100; i++) {
			char name1 = familyNames.charAt(random.nextInt(familyNames.length()));
			char name2 = names.charAt(random.nextInt(names.length()));
			char name3 = names.charAt(random.nextInt(names.length()));
			String name = new String(new char[] {name1,name2,name3});
			nameList.add(name);
			System.out.println(name);
		}
		
		Session session = HibernateUtils.openSession();
		session.getTransaction().begin();
		
		for (String string : nameList) {
			Person person = new Person();
			person.setName(string);
			person.setAge((short) random.nextInt(100));
			session.save(person);
		}
		
		session.getTransaction().commit();
		session.close();
		HibernateUtils.close();
	}
	
	/**
	 * page
	 */
	@Test
	public void run5() {
		Session session = HibernateUtils.openSession();
		session.getTransaction().begin();
		
		Query<Person> query = session.createQuery("from Person", Person.class);
		query.setFirstResult(10);
		query.setMaxResults(10);
		List<Person> persons = query.list();
		System.out.println(persons);
		
		session.getTransaction().commit();
		session.close();
		HibernateUtils.close();
	}
	
}
